package com.jy.adscm.declarationForm.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.bean.system.JyCode;
import com.jy.adscm.declarationForm.bean.JyDeclarationForm;
import com.jy.adscm.declarationForm.bean.JyFormTermManage;
import com.jy.adscm.declarationForm.mapper.JyDeclarationFormMapper;
import com.jy.adscm.declarationForm.mapper.JyFormTermManageMapper;
import com.jy.adscm.declarationForm.service.JyFormTermManageService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.jy.adscm.hospital.bean.*;
import com.jy.adscm.hospital.mapper.*;
import com.jy.adscm.system.mapper.mysql.JyCodeMapper;
import com.jy.adscm.utils.AdscmDateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 报单的过程记录期限管理 服务实现类
 * </p>
 *
 * @author yyy
 * @since 2019-01-26
 */
@Service
public class JyFormTermManageServiceImpl extends ServiceImpl<JyFormTermManageMapper, JyFormTermManage> implements JyFormTermManageService {


    private Logger logger = Logger.getLogger(getClass());
    @Autowired
    JyFormTermManageMapper jyFormTermManageMapper;
    @Autowired
    JyCodeMapper jyCodeMapper;
    @Autowired
    JyHospitalCompeteHospitalMapper jyHospitalCompeteHospitalMapper;
    @Autowired
    JyHospitalEquipmentMapper jyHospitalEquipmentMapper;
    @Autowired
    JyHospitalFunctionalDeptMapper jyHospitalFunctionalDeptMapper;
    @Autowired
    JyHospitalIntentionMapper jyHospitalIntentionMapper;
    @Autowired
    JyHospitalPersonnelTeamMapper jyHospitalPersonnelTeamMapper;
    @Autowired
    JyHospitalRemarksMapper jyHospitalRemarksMapper;
    @Autowired
    JyHospitalScaleMapper jyHospitalScaleMapper;
    @Autowired
    JyDeclarationFormMapper jyDeclarationFormMapper;


    @Override
    public void updateFormTermTime() {

        Wrapper<JyFormTermManage> wrapper = new EntityWrapper<>();
        wrapper.eq("form_status",0);//正在监管的
        List<JyFormTermManage> manages = jyFormTermManageMapper.selectList(wrapper);

        Date now = new Date();

        if (manages != null && manages.size()>0){
            for (JyFormTermManage manage : manages) {
                Long formId = manage.getFormId();
                Date createDate = manage.getCreateDate();
                boolean sameDay = AdscmDateUtils.isSameDay(now, createDate);
                if (! sameDay){
                    //如果创建时间和当前时间是同一天，在不做操作，第二天才做更新操作
                    if (
                            queryCompeteHospitalStatus(formId)  &&
                            queryEquipmentStatus(formId)  &&
                            queryFunctionalStatus(formId)  &&
                            queryIntentionStatus(formId)  &&
                            queryPersonnelTeamStatus(formId)  &&
                            queryRemarksStatus(formId)  &&
                            queryScaleStatus(formId)
                            ){

                        manage.setOverDate(new Date());
                        manage.setFormStatus(1);

                        JyDeclarationForm form = jyDeclarationFormMapper.selectById(formId);
                        Integer earlyWarning = form.getEarlyWarning();
                        if (earlyWarning != null && earlyWarning == 1){
                            form.setEarlyWarning(0);
                            jyDeclarationFormMapper.updateById(form);
                        }

                    }else {
//                        Integer totalTerm = manage.getTotalTerm();
                        Integer surplusTerm = manage.getSurplusTerm();


                        if (surplusTerm > 0){
                            manage.setSurplusTerm(surplusTerm - 1);
                            this.setEarlyWarning(formId,surplusTerm);
                        }else {
                            //已经过期，报单已经废弃
                            manage.setFormStatus(2);
//                            manage.setOverDate(new Date());
                        }

                    }
                    jyFormTermManageMapper.updateById(manage);


                }
            }
        }

    }

    /**
     * 设置预警
     * @param formId
     * @return
     */
    private void setEarlyWarning(Long formId,int surplusTerm){

        List<JyCode> codes = jyCodeMapper.getJyCodeByCodeType("form_process_early_warning");
        if (codes != null && codes.size()>0) {
            JyCode jyCode = codes.get(0);
            String codeStr = jyCode.getCode();
            logger.info("预警期限：" + codeStr );
            int code = Integer.parseInt(codeStr);

            if (surplusTerm <= code){
                JyDeclarationForm form = jyDeclarationFormMapper.selectById(formId);
                form.setEarlyWarning(1);
                jyDeclarationFormMapper.updateById(form);
            }
        }
    }


    /**
     * 查询竞争对手信息条数是否填写完毕
     * @param formId
     * @return
     */
    private boolean queryCompeteHospitalStatus(Long formId){

        logger.info("查询报单ID为【"+formId+"】的竞争对手信息条数是否填写完毕");
        boolean flag = false;
        List<JyCode> codes = jyCodeMapper.getJyCodeByCodeType("form_process_compete_hospital");
        if (codes != null && codes.size()>0){
            JyCode jyCode = codes.get(0);
            String codeStr = jyCode.getCode();
            logger.info("需要填写的竞争对手信息条数为："+codeStr+" 条");
            int code = Integer.parseInt(codeStr);


            Wrapper<JyHospitalCompeteHospital> wrapper = new EntityWrapper<>();
            wrapper.eq("form_id",formId);

            Integer count = jyHospitalCompeteHospitalMapper.selectCount(wrapper);
            logger.info("实际填写的竞争对手信息条数为："+count+" 条");

            if (count >= code){
                flag = true;
            }
        }
        return flag;
    }
    /**
     * 查询设施设备信息条数是否填写完毕
     * @param formId
     * @return
     */
    private boolean queryEquipmentStatus(Long formId){

        logger.info("查询报单ID为【"+formId+"】的设施设备信息条数是否填写完毕");
        boolean flag = false;
        List<JyCode> codes = jyCodeMapper.getJyCodeByCodeType("form_process_equipment");
        if (codes != null && codes.size()>0){
            JyCode jyCode = codes.get(0);
            String codeStr = jyCode.getCode();
            logger.info("需要填写的设施设备信息条数为："+codeStr+" 条");
            int code = Integer.parseInt(codeStr);


            Wrapper<JyHospitalEquipment> wrapper = new EntityWrapper<>();
            wrapper.eq("form_id",formId);

            Integer count = jyHospitalEquipmentMapper.selectCount(wrapper);
            logger.info("实际填写的设施设备信息条数为："+count+" 条");

            if (count >= code){
                flag = true;
            }
        }
        return flag;
    }
    /**
     * 查询其他职能部门条数是否填写完毕
     * @param formId
     * @return
     */
    private boolean queryFunctionalStatus(Long formId){

        logger.info("查询报单ID为【"+formId+"】的其他职能部门信息条数是否填写完毕");
        boolean flag = false;
        List<JyCode> codes = jyCodeMapper.getJyCodeByCodeType("form_process_functional_dept");
        if (codes != null && codes.size()>0){
            JyCode jyCode = codes.get(0);
            String codeStr = jyCode.getCode();
            logger.info("需要填写的其他职能部门信息条数为："+codeStr+" 条");
            int code = Integer.parseInt(codeStr);


            Wrapper<JyHospitalFunctionalDept> wrapper = new EntityWrapper<>();
            wrapper.eq("form_id",formId);

            Integer count = jyHospitalFunctionalDeptMapper.selectCount(wrapper);
            logger.info("实际填写的其他职能部门信息条数为："+count+" 条");

            if (count >= code){
                flag = true;
            }
        }
        return flag;
    }
    /**
     * 查询意向信息条数是否填写完毕
     * @param formId
     * @return
     */
    private boolean queryIntentionStatus(Long formId){

        logger.info("查询报单ID为【"+formId+"】的意向信息条数是否填写完毕");
        boolean flag = false;
        List<JyCode> codes = jyCodeMapper.getJyCodeByCodeType("form_process_intention");
        if (codes != null && codes.size()>0){
            JyCode jyCode = codes.get(0);
            String codeStr = jyCode.getCode();
            logger.info("需要填写的意向信息条数为："+codeStr+" 条");
            int code = Integer.parseInt(codeStr);


            Wrapper<JyHospitalIntention> wrapper = new EntityWrapper<>();
            wrapper.eq("form_id",formId);

            Integer count = jyHospitalIntentionMapper.selectCount(wrapper);
            logger.info("实际填写的意向信息条数为："+count+" 条");

            if (count >= code){
                flag = true;
            }
        }
        return flag;
    }
    /**
     * 查询人员团队信息条数是否填写完毕
     * @param formId
     * @return
     */
    private boolean queryPersonnelTeamStatus(Long formId){

        logger.info("查询报单ID为【"+formId+"】的人员团队信息条数是否填写完毕");
        boolean flag = false;
        List<JyCode> codes = jyCodeMapper.getJyCodeByCodeType("form_process_personnel_team");
        if (codes != null && codes.size()>0){
            JyCode jyCode = codes.get(0);
            String codeStr = jyCode.getCode();
            logger.info("需要填写的人员团队信息条数为："+codeStr+" 条");
            int code = Integer.parseInt(codeStr);


            Wrapper<JyHospitalPersonnelTeam> wrapper = new EntityWrapper<>();
            wrapper.eq("form_id",formId);

            Integer count = jyHospitalPersonnelTeamMapper.selectCount(wrapper);
            logger.info("实际填写的人员团队信息条数为："+count+" 条");

            if (count >= code){
                flag = true;
            }
        }
        return flag;
    }
    /**
     * 查询其他备注信息条数是否填写完毕
     * @param formId
     * @return
     */
    private boolean queryRemarksStatus(Long formId){

        logger.info("查询报单ID为【"+formId+"】的其他备注信息条数是否填写完毕");
        boolean flag = false;
        List<JyCode> codes = jyCodeMapper.getJyCodeByCodeType("form_process_remarks");
        if (codes != null && codes.size()>0){
            JyCode jyCode = codes.get(0);
            String codeStr = jyCode.getCode();
            logger.info("需要填写的其他备注信息条数为："+codeStr+" 条");
            int code = Integer.parseInt(codeStr);


            Wrapper<JyHospitalRemarks> wrapper = new EntityWrapper<>();
            wrapper.eq("form_id",formId);

            Integer count = jyHospitalRemarksMapper.selectCount(wrapper);
            logger.info("实际填写的其他备注信息条数为："+count+" 条");

            if (count >= code){
                flag = true;
            }
        }
        return flag;
    }
    /**
     * 查询医院规模信息条数是否填写完毕
     * @param formId
     * @return
     */
    private boolean queryScaleStatus(Long formId){

        logger.info("查询报单ID为【"+formId+"】的医院规模信息条数是否填写完毕");
        boolean flag = false;
        List<JyCode> codes = jyCodeMapper.getJyCodeByCodeType("form_process_scale");
        if (codes != null && codes.size()>0){
            JyCode jyCode = codes.get(0);
            String codeStr = jyCode.getCode();
            logger.info("需要填写的医院规模信息条数为："+codeStr+" 条");
            int code = Integer.parseInt(codeStr);


            Wrapper<JyHospitalScale> wrapper = new EntityWrapper<>();
            wrapper.eq("form_id",formId);

            Integer count = jyHospitalScaleMapper.selectCount(wrapper);
            logger.info("实际填写的医院规模信息条数为："+count+" 条");

            if (count >= code){
                flag = true;
            }
        }
        return flag;
    }


}
